package com.sgb.kjwl.view.ui.fragment.packers;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;

import com.bumptech.glide.Glide;
import com.luck.picture.lib.decoration.GridSpacingItemDecoration;
import com.luck.picture.lib.tools.ScreenUtils;
import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentShopPersonChildIntroBinding;
import com.sgb.kjwl.di.packers.DaggerPackersFragmentComponent;
import com.sgb.kjwl.di.packers.PackersFragmentModule;
import com.sgb.kjwl.model.entity.ShopTeamInfoEntity;
import com.sgb.kjwl.view.ui.activity.PhotoViewActivity;
import com.sgb.kjwl.view.ui.activity.self.packers.PersonShopActivity;
import com.sgb.kjwl.view.ui.adapter.packers.PresonIntroAdapter;
import com.sgb.kjwl.view.ui.adapter.self.packers.FlowLayoutAdapter0;
import com.sgb.link.view.widget.ExpandedTextView;
import com.sgb.kjwl.viewmodel.self.packers.PersonIntroModel;
import com.swgk.core.base.di.AppComponent;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 *
 * @author WYJ
 * @date 2020/8/31
 * 企业店铺 工队简介
 */
public class PersonIntroFragment extends AppLazyFragment {
    public static final String SHOP_ID = "shop_id";
    public static final String SHOP_NO = "shop_no";
    public static final String CHANNEL_ID = "channel_id";

    @Inject
    PersonIntroModel mModel;
    private FragmentShopPersonChildIntroBinding mBind;

    private String mShopId;
    private String mChannelId;

    private FlowLayoutAdapter0 mAdapter1;
    private FlowLayoutAdapter0 mAdapter2;
    private String mShopNo;

    public static PersonIntroFragment newInstance(String shopId,String no, String id) {
        PersonIntroFragment fragment = new PersonIntroFragment();
        Bundle bundle = new Bundle();
        bundle.putString(SHOP_ID, shopId);
        bundle.putString(SHOP_NO, no);
        bundle.putString(CHANNEL_ID, id);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBind = DataBindingUtil.inflate(inflater, R.layout.fragment_shop_person_child_intro, container, false);
        return mBind.getRoot();
    }


    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerPackersFragmentComponent
                .builder()
                .appComponent(appComponent)
                .packersFragmentModule(new PackersFragmentModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initView() {
        mShopId = getArguments().getString(SHOP_ID);
        mShopNo = getArguments().getString(SHOP_NO);
        mChannelId = getArguments().getString(CHANNEL_ID);

       /* mAdapter1 = new FlowLayoutAdapter0(getActivity(), new ArrayList<>());
        mBind.rvBusiness.setLayoutManager(new FlowLayoutManager());
        mBind.rvBusiness.addItemDecoration(new MyItemDecoration());
        mBind.rvBusiness.setAdapter(mAdapter1);*/

      /*  mAdapter2 = new FlowLayoutAdapter0(getActivity(), new ArrayList<>());
        mBind.rvArea.setLayoutManager(new FlowLayoutManager());
        mBind.rvArea.addItemDecoration(new MyItemDecoration());
        mBind.rvArea.setAdapter(mAdapter2);*/
    }

    @Override
    protected void onLazyLoad() {
        mModel.getData(mShopId, mShopNo,mChannelId);
    }

    public void showEmpty() {
        mBind.emptyView.setVisibility(View.VISIBLE);
    }

    public void updateViews(ShopTeamInfoEntity shopTeamInfoEntity) {
        if (shopTeamInfoEntity == null) {
            showEmpty();
            return;
        }
        //合作单位布局处理
        //如果没有合作单位那么就隐藏合作单位布局文件，否则显示
        if (shopTeamInfoEntity.getTeamType()==0){//施工队
          if (!TextUtils.isEmpty(shopTeamInfoEntity.getTrustCompanyInfo().getLegalName())){
              mBind.llCooperationCompany.setVisibility(View.VISIBLE);
          }else {
              mBind.llCooperationCompany.setVisibility(View.GONE);
          }
        }else {
            mBind.llCooperationCompany.setVisibility(View.GONE);
        }
        //营业执照
        if (!TextUtils.isEmpty(shopTeamInfoEntity.getTrustCompanyInfo().getBusinessLicense())){
            Glide.with(this).load(shopTeamInfoEntity.getTrustCompanyInfo().getBusinessLicense()).into(mBind.businessLicense);
        }
        mBind.businessLicense.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //营业执照放大
                List<String>   imagelists = new ArrayList<>();
                imagelists.add(shopTeamInfoEntity.getTrustCompanyInfo().getBusinessLicense());
                Intent intent = new Intent(getActivity(), PhotoViewActivity.class);
                intent.putExtra(PhotoViewActivity.KEY_PHOTO_URLS, (Serializable)imagelists);
                intent.putExtra(PhotoViewActivity.KEY_PHOTO_POSITION, 0);
                getActivity().startActivity(intent);
            }
        });
        //合作协议与委托书处理
        List<String> filePath = shopTeamInfoEntity.getTrustCompanyInfo().getFilePath();
        if (filePath!=null&&filePath.size()>0){
            GridLayoutManager manager = new GridLayoutManager( mBind.recyclerview.getContext(), 3);
            mBind.recyclerview.setLayoutManager(manager);
            mBind.recyclerview.addItemDecoration(new GridSpacingItemDecoration(4, ScreenUtils.dip2px(getActivity(), 5), false));
            mBind.recyclerview.setAdapter(new PresonIntroAdapter(mBind.recyclerview.getContext(), filePath));
        }

        mBind.emptyView.setVisibility(View.GONE);
        mBind.setViewModel(shopTeamInfoEntity);
        mBind.tvTeamNum.setText(shopTeamInfoEntity.getTeamSize() + "人");
        String categoryName = shopTeamInfoEntity.getCategoryName();
        if(!TextUtils.isEmpty(categoryName)){
            mBind.tiBusinessText.setText(categoryName.replaceAll(","," | "));
        }else {
            mBind.tiBusinessText.setText("");
        }

        String address = shopTeamInfoEntity.getServiceAreaName();
        if (!TextUtils.isEmpty(address)){
            mBind.tvServiceArea.setText(address.replaceAll("、"," | "));
        }
        new ExpandedTextView().showTextT(mBind.tvBasic, mBind.tvBasicExpand);
        new ExpandedTextView().showTextT(mBind.tvHistory, mBind.tvHistoryExpand);
        new ExpandedTextView().showTextT(mBind.tvStructure, mBind.tvStructureExpand);
        new ExpandedTextView().showTextT(mBind.tvIdea, mBind.tvIdeaExpand);
        if (getActivity() != null) {
            ((PersonShopActivity) getActivity()).updatePersonInfo(shopTeamInfoEntity);
        }
    }
}
